package com.iot.ammeter.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.iot.ammeter.IApplication
import com.iot.ammeter.model.Payment
import com.iot.ammeter.model.entry.BalanceFillRecord
import com.iot.ammeter.network.Repository
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList
import kotlin.random.Random

class BalanceFillViewModel : ViewModel() {

    private val _records = MutableStateFlow<ArrayList<BalanceFillRecord>>(ArrayList())
    val records: StateFlow<List<BalanceFillRecord>> = _records

    init {
        getRecordsWithStorage()
    }

    private fun getRecordsWithStorage() = viewModelScope.launch(Dispatchers.IO) {
        val recordsR = IApplication.db.balanceFillRecordDao().getAll()
        // 虚假数据
        val records = mutableListOf<BalanceFillRecord>()
        val payments = arrayOf(Payment.WeChat, Payment.AliPay)
        repeat(10) {
            records.add(
                BalanceFillRecord(
                    it.toString(),
                    Random.nextInt(50, 80).toFloat(),
                    Date().time,
                    payments.random()
                )
            )
        }
        _records.value.addAll(records)
    }

    private fun updateRecordsWithNetwork(
        onError: (Exception) -> Unit
    ) {
        viewModelScope.launch {
            val expect = _records.value
        }
    }

}